package com.ruoyi.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.enums.AuditStatus;
import com.ruoyi.common.enums.DelFlag;
import com.ruoyi.common.enums.TuiHuoStatus;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.ChuKuDanAddRequest;
import com.ruoyi.system.domain.vo.ChuKuDanAuditRequest;
import com.ruoyi.system.domain.vo.ChuKuDanQuery;
import com.ruoyi.system.mapper.ChuKuDanDetailMapper;
import com.ruoyi.system.mapper.DingDanDetailMapper;
import com.ruoyi.system.mapper.HuoPinMapper;
import com.ruoyi.system.mapper.RuKuDanMapper;
import com.ruoyi.system.service.ICaiGouDanService;
import com.ruoyi.system.service.IChuKuDanDetailService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.service.IDingDanService;
import com.ruoyi.system.service.IRuKuDanService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 出库单详情信息 服务实现类
 * </p>
 *
 * @author qubaofeng
 * @since 2025-02-23
 */
@Service
public class ChuKuDanDetailServiceImpl extends ServiceImpl<ChuKuDanDetailMapper, ChuKuDanDetail> implements IChuKuDanDetailService {


    @Resource
    private HuoPinMapper huoPinMapper;

    @Resource
    private ChuKuDanDetailMapper chuKuDanDetailMapper;

    @Resource
    private IRuKuDanService ruKuDanService;

    @Resource
    private IDingDanService dingDanService;

    @Resource
    private ICaiGouDanService caiGouDanService;

    @Resource
    private DingDanDetailMapper dingDanDetailMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized boolean add(ChuKuDanAddRequest request, String username) {

        BigDecimal chuKuCount = new BigDecimal(0);

        for(ChuKuDanDetail dingDanDetail : request.getDetails()){
            ChuKuDanDetail chuKuDanDetail = new ChuKuDanDetail();
            chuKuDanDetail.setDingDanDetailId(dingDanDetail.getId());
            chuKuDanDetail.setChuKuCount(dingDanDetail.getChuKuCount());
            chuKuDanDetail.setHuoPinId(dingDanDetail.getHuoPinId());
            chuKuDanDetail.setChuKuPrice(dingDanDetail.getChuKuPrice());
            chuKuDanDetail.setChuKuShiJian(new Date());
            chuKuDanDetail.setCreateBy(username);
            chuKuDanDetail.setCreateTime(new Date());
            chuKuDanDetail.setStatus(AuditStatus.INIT.getCode());
            chuKuDanDetail.setDelFlag(DelFlag.NOMAL.getCode());
            chuKuDanDetailMapper.insert(chuKuDanDetail);
            chuKuCount = chuKuCount.add(dingDanDetail.getChuKuCount());
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized boolean modify(ChuKuDanDetail dingDanDetail, String username) {

        ChuKuDanDetail chuKuDanDetail = new ChuKuDanDetail();
        chuKuDanDetail.setId(dingDanDetail.getId());
        chuKuDanDetail.setChuKuCount(dingDanDetail.getChuKuCount());
        chuKuDanDetail.setChuKuPrice(dingDanDetail.getChuKuPrice());
        chuKuDanDetail.setUpdateBy(username);
        chuKuDanDetail.setUpdateTime(new Date());
        return chuKuDanDetailMapper.updateById(chuKuDanDetail) >0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized boolean audit(ChuKuDanAuditRequest request, String username) {

        LambdaQueryWrapper<ChuKuDanDetail> q1 = new LambdaQueryWrapper<>();
        q1.eq(ChuKuDanDetail::getId, request.getId());
        q1.eq(ChuKuDanDetail::getDelFlag, DelFlag.NOMAL.getCode());
        List<ChuKuDanDetail> details = chuKuDanDetailMapper.selectList(q1);
        for (ChuKuDanDetail d : details) {
            if(!AuditStatus.INIT.getCode().equals(d.getStatus())){
                continue;
            }
            DingDanDetail dingDanDetail = new DingDanDetail();
            dingDanDetail.setId(d.getDingDanDetailId());
            dingDanDetail.setRealBuyCount(d.getChuKuCount());
            dingDanDetailMapper.updateById(dingDanDetail);

            if(request.getStatus().equals(AuditStatus.PASS.getCode())) {
                // 如果审核通过，同时修改货品的库存
                HuoPin huoPin = huoPinMapper.selectById(d.getHuoPinId());
                BigDecimal kuCun = huoPin.getKuCun();
                kuCun = kuCun.subtract(d.getChuKuCount());
                huoPin.setKuCun(kuCun);
                huoPinMapper.updateById(huoPin);
            }
        }


        return true;
    }

    @Override
    public void createByRukuDan(RuKuDan ruKuDan) {
        ChuKuDanDetail chuKuDanDetail = new ChuKuDanDetail();
        chuKuDanDetail.setDingDanDetailId(ruKuDan.getDingDanDetailId());
        chuKuDanDetail.setChuKuCount(ruKuDan.getRuKuCount());
        chuKuDanDetail.setChuKuPrice(ruKuDan.getRuKuPrice());
        chuKuDanDetail.setChuKuShiJian(ruKuDan.getUpdateTime());
        chuKuDanDetail.setHuoPinId(ruKuDan.getHuoPinId());
        chuKuDanDetail.setCreateTime(new Date());
        chuKuDanDetail.setDelFlag(DelFlag.NOMAL.getCode());
        chuKuDanDetailMapper.insert(chuKuDanDetail);
    }

    @Override
    public List<DingDanDetail> selectChuKuDanDetailList(ChuKuDanQuery req) {
        return chuKuDanDetailMapper.selectChuKuDanDetailList(req);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized boolean tuiKu(ChuKuDanDetail info) {

        ChuKuDanDetail oldData = chuKuDanDetailMapper.selectById(info.getId());

        // 修改出库单退货状态
        ChuKuDanDetail data = new ChuKuDanDetail();
        data.setId(info.getId());
        data.setTuiHuo(TuiHuoStatus.ROLLBACK.getCode());
        int row = chuKuDanDetailMapper.updateById(data);

        // 修改库存量，将出库单的数量退入库存
        HuoPin huoPin = huoPinMapper.selectById(oldData.getHuoPinId());
        BigDecimal kuCun = huoPin.getKuCun();
        kuCun = kuCun.add(oldData.getChuKuCount());
        huoPin.setKuCun(kuCun);

        huoPinMapper.updateById(huoPin);
        return row >0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized boolean tuiHuo(ChuKuDanDetail info) {

        ChuKuDanDetail oldData = chuKuDanDetailMapper.selectById(info.getId());

        // 修改出库单退货状态
        ChuKuDanDetail data = new ChuKuDanDetail();
        data.setId(info.getId());
        data.setTuiHuo(TuiHuoStatus.ROLLBACK.getCode());
        int row = chuKuDanDetailMapper.updateById(data);

        RuKuDan ruKuDan = ruKuDanService.selectByDingDanDetailId(oldData.getDingDanDetailId());
        ruKuDan.setTuiHuo(TuiHuoStatus.ROLLBACK.getCode());
        ruKuDanService.updateById(ruKuDan);

        CaiGouDan caiGouDan = caiGouDanService.selectByDingDanDetailId(oldData.getDingDanDetailId());
        caiGouDan.setTuiHuo(TuiHuoStatus.ROLLBACK.getCode());
        caiGouDanService.updateById(caiGouDan);

        DingDanDetail detail = dingDanDetailMapper.selectById(oldData.getDingDanDetailId());
        detail.setTuiHuo(TuiHuoStatus.ROLLBACK.getCode());
        dingDanDetailMapper.updateById(detail);

        return row >0;
    }

    @Override
    public ChuKuDanDetail getChuKuDanDetail(Long id) {
        return baseMapper.selectInfoById(id);
    }
}
